local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local theme = require("theme")
local dpi	= require("beautiful.xresources").apply_dpi




local indicator = {}



indicator.widget = wibox.widget{
	{
		{
			{
				{
					{
						{
							text	= "F",
							valign = "center",
							halign = "center",
							font    = theme.indicator_font,
							widget	= wibox.widget.textbox,
						},
						shape = function(cr, width, height)
							gears.shape.rounded_rect(cr, width, height, theme.indicator_radius)
						end,
						fg = theme.indicator_floating_fg,
						bg = theme.indicator_floating_bg,
						--forced_width = theme.indicator_width,
						--forced_height = theme.indicator_width,
						widget = wibox.container.background,
					},
					id = "mode",
					visible = false,
					margins = theme.indicator_margins,
					widget  = wibox.container.margin,
				},
				{
					{
						{
							text	= "T",
							valign = "center",
							halign = "center",
							font    = theme.indicator_font,
							widget	= wibox.widget.textbox,
						},
						shape = function(cr, width, height)
							gears.shape.rounded_rect(cr, width, height, theme.indicator_radius)
						end,
						fg = theme.indicator_ontop_fg,
						bg = theme.indicator_ontop_bg,
						--forced_width = theme.indicator_width,
						--forced_height = theme.indicator_width,
						widget = wibox.container.background,
					},
					id = "mode",
					visible = false,
					margins = theme.indicator_margins,
					widget  = wibox.container.margin,
				},
				{
					{
						{
							text	= "S",
							valign = "center",
							halign = "center",
							font    = theme.indicator_font,
							widget	= wibox.widget.textbox,
						},
						shape = function(cr, width, height)
							gears.shape.rounded_rect(cr, width, height, theme.indicator_radius)
						end,
						fg = theme.indicator_sticky_fg,
						bg = theme.indicator_sticky_bg,
						--forced_width = theme.indicator_width,
						--forced_height = theme.indicator_width,
						widget = wibox.container.background,
					},
					id = "mode",
					visible = false,
					margins = theme.indicator_margins,
					widget  = wibox.container.margin,
				},
				{
					{
						{
							text	= "M",
							valign = "center",
							halign = "center",
							font    = theme.indicator_font,
							widget	= wibox.widget.textbox,
						},
						shape = function(cr, width, height)
							gears.shape.rounded_rect(cr, width, height, theme.indicator_radius)
						end,
						fg = theme.indicator_maximized_fg,
						bg = theme.indicator_maximized_bg,
						--forced_width = theme.indicator_width,
						--forced_height = theme.indicator_width,
						widget = wibox.container.background,
					},
					id = "mode",
					visible = false,
					margins = theme.indicator_margins,
					widget  = wibox.container.margin,
				},
				layout = wibox.layout.fixed.horizontal,
			},
			--top = dpi(2),
			--bottom = dpi(2),
			--left   = dpi(5),
			right  = dpi(12),
			widget	= wibox.container.margin,
		},
		id = "background",
		fg = theme.widget_fg,
		bg = theme.indicator_bg,
		shape = function(cr, width, height)
			gears.shape.rounded_rect(cr, width, height, theme.widget_radius)
			--gears.shape.rounded_rect(cr, width, height, dpi(20))
		end,
		widget = wibox.container.background,
	},
	margins = theme.widget_margins,
	widget	= wibox.container.margin,
	visible = false,
	set_bg = function(self, var)
		self:get_children_by_id("background")[1].bg = var
	end,
	set_fg = function(self, var)
		self:get_children_by_id("background")[1].fg = var
	end
}



function indicator:clear()
	self.widget.visible = false
	--local m = self.widget:get_children_by_id("mode")
	--m[1].visible = false
	--m[2].visible = false
	--m[3].visible = false
	--m[4].visible = false
end



function indicator:update()
	local c = client.focus
	if c ~= nil then
		local m = self.widget:get_children_by_id("mode")
		m[1].visible = c.floating
		m[2].visible = c.ontop
		m[3].visible = c.sticky
		m[4].visible = c.maximized
		if c.floating or c.ontop or c.sticky or c.maximized then
			self.widget.visible = true
		else
			self.widget.visible = false
		end
	else
		self:clear()
	end
end




function indicator:setup()
	--self.widget:connect_signal('mouse::enter',function() 
	--	self.widget.bg = theme.widget_bg_hover
	--end)
	--self.widget:connect_signal('mouse::leave',function() 
	--	self.widget.bg = theme.widget_bg
	--end)

	local m = self.widget:get_children_by_id("mode")


	m[1]:buttons(awful.util.table.join(
		awful.button({}, 1, nil, function() 
			local c = client.focus
			if c == nil then return end
			c.floating = not c.floating
			self:update()
		end)
	))
	m[2]:buttons(awful.util.table.join(
		awful.button({}, 1, nil, function() 
			local c = client.focus
			if c == nil then return end
			c.ontop = not c.ontop
			self:update()
		end)
	))
	m[3]:buttons(awful.util.table.join(
		awful.button({}, 1, nil, function() 
			local c = client.focus
			if c == nil then return end
			c.sticky = not c.sticky
			self:update()
		end)
	))
	m[4]:buttons(awful.util.table.join(
		awful.button({}, 1, nil, function() 
			local c = client.focus
			if c == nil then return end
			c.maximized = not c.maximized
			self:update()
		end)
	))

	client.connect_signal("untagged", function(c) 
		local s = awful.screen.focused()
		if #s.clients == 0 then
			indicator:clear()
		end
	end)

	client.connect_signal("unfocus", function(c) 
		local s = awful.screen.focused()
		if #s.clients == 0 then
			indicator:clear()
		end
	end)

	client.connect_signal("focus", function(c) 
		indicator:update()
	end)

	client.connect_signal("property::floating", function(c) 
		indicator:update()
	end)

	client.connect_signal("property::maximized", function(c) 
		indicator:update()
	end)

	client.connect_signal("property::sticky", function(c) 
		indicator:update()
	end)

	client.connect_signal("property::ontop", function(c) 
		indicator:update()
	end)

	return self.widget
end


return indicator
